package com.koron.css2.newProjectCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.meterService.mapper.MsInfoMapper;
import com.koron.css2.newProjectCenter.Enum.PjInfoProcessStateEnum;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.bean.PjMeter;
import com.koron.css2.newProjectCenter.bean.query.PjMeterQuery;
import com.koron.css2.newProjectCenter.exception.UserNoException;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.newProjectCenter.mapper.PjMeterMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.mapper.UserDiscountMapper;
import com.koron.css2.serviceManage.mapper.UserMixtureMapper;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;

/**
 * 工程管理-单条立户信息新增
 */
public class PjMeterInsert implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            PjMeter pjMeter = JsonUtils.objectToPojo(req.getData(), PjMeter.class);
            PjMeterMapper pjMeterMapper = factory.getMapper(PjMeterMapper.class);
            UserMixtureMapper userMixtureMapper = factory.getMapper(UserMixtureMapper.class);
            UserDiscountMapper userDiscountMapper = factory.getMapper(UserDiscountMapper.class);
            CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
            AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);
            MsInfoMapper msInfoMapper = factory.getMapper(MsInfoMapper.class);
            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
            UserInfoMapper userInfoMapper = factory.getMapper(UserInfoMapper.class);

            PjInfoBean pjInfoBean = pjInfoMapper.selectById(pjMeter.getConsultId());
            String pjProcessState = pjInfoBean.getProcessState();
            PjSave pjSave = new PjSave();
            String userNo = "";
            Integer singleCustomerFlag = pjMeter.getSingleCustomerFlag();

            // 1. pjMeter: id,createAccount,createTime,createName,tenantId
            pjMeter.setCreateInfo(userInfo);
            // 1.1 pjMeter: msInfoId
            String meterNo = pjMeter.getUserInfo().getMeterNo();
            // 1.1.1 立户阶段水表编码必须存在;
            if (pjProcessState.equals(PjInfoProcessStateEnum.REGISTER_ACCOUNT.getProcessState())) {
                if (StringUtils.isEmpty(meterNo)) {
                    return MessageBean.create(Constant.MESSAGE_INT_FAIL, "立户阶段水表编码必须存在", null);
                }
            }
            int iCount = pjMeterMapper.countMeterNo(pjMeter.getConsultId(), meterNo);
            if (iCount > 0) {
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, "水表编号 " + meterNo + "已存在", null);
            }
            String msInfoId = msInfoMapper.getIdByMeterNo(meterNo);
            pjMeter.setMsInfoId(msInfoId);
            // 如果是一个客户+一个账户对应多个账户的情况
            if (singleCustomerFlag != null && singleCustomerFlag == 1) {
                CtmInfoBean ctmInfoBean = pjMeter.getCtmInfo();
                if (StringUtils.isNotEmpty(ctmInfoBean.getClientAddrId())) {
                    ctmInfoBean.setClientAddr(ctmInfoBean.getClientAddrId());
                }
                if(StringUtils.isEmpty(ctmInfoBean.getCtmAddr())){
                    ctmInfoBean.setCtmAddr(ctmInfoBean.getClientAddrDetail());
                }
                ctmInfoMapper.ctmInfoUpdate(ctmInfoBean, CtmInfoBean.CTMTEMP);
                pjMeter.setUserCtmTempId(ctmInfoBean.getId());
                // 3. 更新临时账户信息;pjMeter:userAccountTempId
                AccountInfoBean accountInfoBean = pjMeter.getAccountInfo();
                accountInfoMapper.accountInfoUpdate(accountInfoBean, AccountInfoBean.ACCOUNTTEMP);
                pjMeter.setUserAccountTempId(accountInfoBean.getId());
                // 4. 保存临时用户信息
                UserBean userBean = pjMeter.getUserInfo();
                // 4.1 判断是否是立户的第一条数据
                Boolean noMeterFlag = false;
                if (pjProcessState.equals(PjInfoProcessStateEnum.REGISTER_ACCOUNT.getProcessState())) {
                    PjMeterQuery pjMeterQuery = new PjMeterQuery();
                    pjMeterQuery.setConsultId(pjMeter.getConsultId());
                    List<PjMeter> pjMeterList = pjMeterMapper.getList(pjMeterQuery);
                    if (pjMeterList != null && pjMeterList.size() == 0) {
                        noMeterFlag = true;
                    }
                }
                // 4.2 保存临时用户信息，获得userNo
                userNo = pjSave.saveMeterUserInfoTemp(factory, userInfo, pjMeter, ctmInfoBean, accountInfoBean, pjMeter.getMsInfo());
                // 4.3 根据情况，根据userNo同步ctmNo，accountNo
                if (noMeterFlag) {
                    String ctmNo = Tools.getSameCtmNo(userNo);
                    String accountNo = Tools.getSameAccountNo(userNo);
                    ctmInfoBean.setCtmNo(ctmNo);
                    accountInfoBean.setAccountNo(accountNo);
                    ctmInfoMapper.ctmInfoUpdate(ctmInfoBean, CtmInfoBean.CTMTEMP);
                    accountInfoMapper.accountInfoUpdate(accountInfoBean, AccountInfoBean.ACCOUNTTEMP);
                    // 4.4 将新的客户编号和账户编号回填到临时用户中
                    userBean.setCtmNo(ctmNo);
                    userBean.setAccountNo(accountNo);
                    userBean.setUpdateInfo(userInfo);
                    userInfoMapper.userInfoUpdate(userBean, UserBean.USERTEMP);
                }
            }
            // 如果是一个客户+一个账户对应一个账户的情况
            if (singleCustomerFlag != null && singleCustomerFlag == 0) {
                CtmInfoBean ctmInfoBean = pjMeter.getCtmInfo();
                AccountInfoBean accountInfoBean = pjMeter.getAccountInfo();
                // 4.5 ctmInfoBean:clientAddrId->clientAddr
                if (StringUtils.isNotEmpty(ctmInfoBean.getClientAddrId())) {
                    ctmInfoBean.setClientAddr(ctmInfoBean.getClientAddrId());
                }
                // 4.6 ctmInfoBean: id, createTime, createAccount, createName, tenantId
                ctmInfoBean.setCreateInfo(userInfo);
                // 4.7 accountInfoBean: id, createTime, createAccount, createName, tenantId
                accountInfoBean.setCreateInfo(userInfo);
                // 4.8 保存临时用户信息，获取userNo
                userNo = pjSave.saveMeterUserInfoTemp(factory, userInfo, pjMeter, ctmInfoBean, accountInfoBean, pjMeter.getMsInfo());
                // 4.9 根据userNo同步ctmNo,accountNo
                String ctmNo = Tools.getSameCtmNo(userNo);
                String accountNo = Tools.getSameAccountNo(userNo);
                ctmInfoBean.setCtmNo(ctmNo);
                accountInfoBean.setAccountNo(accountNo);
                if(StringUtils.isEmpty(ctmInfoBean.getCtmAddr())){
                    ctmInfoBean.setCtmAddr(ctmInfoBean.getClientAddrDetail());
                }
                ctmInfoMapper.ctmInfoAdd(ctmInfoBean, CtmInfoBean.CTMTEMP);
                accountInfoMapper.accountInfoAdd(accountInfoBean, AccountInfoBean.ACCOUNTTEMP);
                // 4.10 将新的客户编号和账户编号回填到临时用户中
                UserBean userBean = pjMeter.getUserInfo();
                userBean.setCtmNo(ctmNo);
                userBean.setAccountNo(accountNo);
                // 4.11 userBean: id, createTime, createAccount, createName, tenantId
                userBean.setUpdateInfo(userInfo);
                userInfoMapper.userInfoUpdate(userBean, UserBean.USERTEMP);
                // 4.12 pjMeter: userCtmTempId, userAccountTempId
                pjMeter.setUserCtmTempId(ctmInfoBean.getId());
                pjMeter.setUserAccountTempId(accountInfoBean.getId());
            }
            // 5. 保存混合用水信息
            pjSave.saveMeterUserMixture(userMixtureMapper, userInfo, pjMeter, userNo);
            // 6. 保存费用折扣
            pjSave.saveMeterUserDiscount(userDiscountMapper, userInfo, pjMeter, userNo);
            // 7. 工程立户数据入库
            pjMeterMapper.insert(pjMeter);
            // 8. 返回id
            MessageBean<String> messageBean = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "立户单条数据新增成功", String.class);
            messageBean.setData(pjMeter.getId());
            return messageBean;
        } catch (UserNoException e) {
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, e.getMessage(), null);
        } catch (Exception e) {
            logger.error("立户单条数据新增失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "立户单条数据新增失败", null);
        }
    }
}
